var tsc_timeline=function(parent){
  this.version='0.0.1';

	this.editor=parent;
	this.cur=[false,false];
	this.domObj=false;
	
	this.init=function(){
	  var t=document.getElementById( this.editor.config.timelineId );
	  if( typeof(t)=='undefined' || !t ) return this.err('DOM object for Timeline undefined');
		this.domObj=t;
		return true;
	}
	if(!this.init()) return false;
	
	this.init_project=function(){
	  this.apply();
		this.editor.on('cellset', function(prm, obj){obj.iface.timeline.displayActive();});
		//this.editor.on('timelineChange', function(prm, obj){obj.iface.timeline.build_timeline();});
	  return true;
	}
	
	this.apply=function(){
	  return this.build_timeline();
	}

	//Построение таймлайна по данным в проекте
	this.build_timeline=function(){
	  var lines={
	    topline_html:'',
	    levels:[],
	    labels:[]
		};

	  for(var fnum=0,cnt=this.editor.project.data.frameOrd.length; fnum<cnt; fnum++ ){
	    for(var lnum=0,jcnt=this.editor.project.data.layerOrd.length; lnum<jcnt; lnum++ ){
	      if(typeof(lines.levels[lnum])=='undefined') lines.levels[lnum]='';

        if(this.editor.layers.visibility(lnum)){
          var vis_act=false, vis_class='visible';
				}else{
				  var vis_act=true, vis_class='invisible';
				}

	      lines.levels[lnum]+='<a href="#" id="cell_'+fnum+'_'+lnum+'" class="cell '+vis_class+'" onClick="ts_editor.get_canvas('+fnum+','+lnum+'); return false;"><i>'+fnum+'</i></a>';
	      if( typeof( lines.labels[lnum] )=='undefined' ){
					lines.labels[lnum]='<label><a href="" class="vis '+vis_class+'" onClick="ts_editor.layers.visibility('+lnum+','+ vis_act +'); return false;">'+vis_class+'</a></label>';
				}
	    }
		}

		var frames='';
		for(var i in lines.levels){
		  if( typeof(lines.levels[i])=='function' ) continue;
		  frames+='<div class="layer" id="layer_'+i+'">'+lines.levels[i]+'</div>';
		}

		var framesDom=document.getElementById('ts_timeline_frames'); //#ts_timeline_frames
		framesDom.innerHTML=frames;
		
		var framesDom=document.getElementById('ts_timeline_labels');
		framesDom.innerHTML=lines.labels.join('');
		

    this.displayActive();
		delete frames;
		delete lines;
		return true;
	}
	
	this.displayActive=function(){
	  //console.log('displayActive',this.editor.current.frame,this.editor.current.layer);
	  if( this.editor.current.frameNum===false || this.editor.current.layerNum===false ){
			return false;
		}
		var tlObj=document.getElementById('ts_timeline');
		var t=tlObj.getElementsByClassName('active');
		
		if(t.length>0) for(var i=0; i<t.length; i++) t[i].removeClassName('active');

		var cell=document.getElementById('cell_'+this.editor.current.frameNum+'_'+this.editor.current.layerNum);
		if(!cell){
		  return false;
		}else{
		  cell.addClassName('active');
		}
		return true;
	}
}